package classrecord.classes.database.studentnametbl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;

import classrecord.classes.database.common.IEveryTableDao;

public class STUDENTNAMETBLDao implements IEveryTableDao {

	private static final long serialVersionUID = -6303915173706232012L;

	private STUDENTNAMETBLDomain studentnametblDomain;
	private STUDENTNAMETBLDomain recordFoundDomain;

	private Connection connection;
	private PreparedStatement preparedStatement;
	private ResultSet resultSet;

	private final String RECORD_NUMBER = STUDENTNAMETBLColumn.RECORD_NUMBER.get();
	private final String STUDENT_NUMBER = STUDENTNAMETBLColumn.STUDENT_NUMBER.get();
	private final String LAST_NAME = STUDENTNAMETBLColumn.LAST_NAME.get();
	private final String GIVEN_NAME = STUDENTNAMETBLColumn.GIVEN_NAME.get();
	private final String MIDDLE_NAME = STUDENTNAMETBLColumn.MIDDLE_NAME.get();
	private final String MIDDLE_INITIAL = STUDENTNAMETBLColumn.MIDDLE_INITIAL.get();
	private final String NAME_EXTENSION = STUDENTNAMETBLColumn.NAME_EXTENSION.get();
	private final String IUD_FLAG = STUDENTNAMETBLColumn.IUD_FLAG.get();
	private final String CREATE_PID = STUDENTNAMETBLColumn.CREATE_PID.get();
	private final String CREATE_DATETIME = STUDENTNAMETBLColumn.CREATE_DATETIME.get();
	private final String DELETE_PID = STUDENTNAMETBLColumn.DELETE_PID.get();
	private final String DELETE_TIMESTAMP = STUDENTNAMETBLColumn.DELETE_TIMESTAMP.get();
	private final String UPDATE_PID = STUDENTNAMETBLColumn.UPDATE_PID.get();
	private final String UPDATE_TIMESTAMP = STUDENTNAMETBLColumn.UPDATE_TIMESTAMP.get();

	private final String NO_SPACE = "";

	public STUDENTNAMETBLDomain getStudentnametblDomain() {
		return studentnametblDomain;
	}

	public void setStudentnametblDomain(
			STUDENTNAMETBLDomain studentnametblDomain) {
		this.studentnametblDomain = studentnametblDomain;
	}

	public STUDENTNAMETBLDomain getRecordFoundDomain() {
		return recordFoundDomain;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	@Override
	public boolean add() {
		boolean wasInserted = false;
		String studentNumber = studentnametblDomain.getStudentNumber();
		
		String lastName = studentnametblDomain.getLastName();
		String givenName = studentnametblDomain.getGivenName();
		
		String middleName = studentnametblDomain.getMiddleName();
		if (null == middleName) { middleName = NO_SPACE; }
		
		String middleInitial = studentnametblDomain.getMiddleInitial();
		if (null == middleInitial) { middleInitial = NO_SPACE; }
		
		String nameExtension = studentnametblDomain.getNameExtension();
		if (null == nameExtension) { nameExtension = NO_SPACE; }
		
		String insertSql = "INSERT INTO STUDENTNAMETBL (STUDENT_NUMBER, LAST_NAME, GIVEN_NAME, MIDDLE_NAME, MIDDLE_INITIAL, NAME_EXTENSION) VALUES (?, ?, ?, ?, ?, ?);";
		
		try {			
			preparedStatement = connection.prepareStatement(insertSql);
			preparedStatement.setString(1, studentNumber);
			preparedStatement.setString(2, lastName);
			preparedStatement.setString(3, givenName);
			preparedStatement.setString(4, middleName);
			preparedStatement.setString(5, middleInitial);
			preparedStatement.setString(6, nameExtension);
			
			int recordInserted = preparedStatement.executeUpdate();
			wasInserted = recordInserted > 0;

		} catch (SQLException sqle) {
			sqle.printStackTrace();
			
		} finally {
			DbUtils.closeQuietly(preparedStatement);

		}

		return wasInserted;
	}

	@Override
	public boolean update() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean physicalDelete() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean logicalDelete() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 *  ***** Logically retrieve the details of a record. **********************
	 * [1]: This would retrieve the record of a corresponding row by asserting
	 * specific condition/s. [2]: The record's LOGICAL_DELETE_FLAG should not be
	 * equal to 1 for the details to be retrievable.
	 * **********************************************************************
	 */
	public STUDENTNAMETBLDomain logicalRetrieve() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 *  ***** Physically retrieve the details of a record.**********************
	 * [1]: Only the administrator is advise to call this method. [2]: Even a
	 * logically–deleted record could be view once this method has been called.
	 * **********************************************************************
	 */
	public STUDENTNAMETBLDomain physicalRetrieve() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getPhysicalRecordCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getLogicalRecordCount() {
		// TODO Auto-generated method stub
		return 0;
	}

}
